$(function () {
    $("#datepicker").val("");
    $("#datepicker2").val("");
    $("[name='paymentType']").val("");
    $("[name='transferType']").val("");
    loading();
    var f = getQueryString("flag");
    if (f == 1 || f == '1' || f == 2 || f == '2' || f == 3 || f == '3' || f == 4 || f == '4') {
        $("[name='transferType'] option").each(function () {
            if (f == $(this).val()) {
                $(this).attr("selected", true);
            }
        });
        ajaxscoreDetail(1, null, f, null, null);
    } else if (f == null || f == '') {
        ajaxscoreDetail(1, null, null, null, null);
    } else {
        ajaxscoreDetail(1, null, null, null, null);
    }
    $('#datepicker, #datepicker2').bind('', function () {
        validateTime();
    });
});

/**
 * 验证开始时间是否大于结束时间
 */
function validateTime() {
    var startTime = $("#datepicker").val();
    var endTime = $("#datepicker2").val();
    if (isNotEmpty(startTime) && isNotEmpty(endTime)) {
        if (startTime > endTime) {
            alertBox("开始时间不能大于结束时间", "error");
        }
    }
}

/**
 * 积分交易明细    查询
 * @param page            当前页码
 * @param paymentType    支出（收支）类型    1 收入 -1 支出
 * @param transferType    交易类型        0:合约广告 1:好友转账 2:应用购买 3:应用充值 4:专项广告
 * @param startTime        开始时间
 * @param endTime        结束时间
 */
function ajaxscoreDetail(page, paymentType, transferType, startTime, endTime) {
    var url = "/Lvscore_User_M/adScore/ajaxscoreDetail";
    $.post(
        url,
        {
            'page': page,
            'paymentType': paymentType,
            'transferType': transferType,
            'startTime': startTime,
            'endTime': endTime
        },
        function (data) {
            if (data.nowpage == 1) {
                $(".tcdPageCode").empty().unbind();
            }
            $("#info").empty();
            if (data.scoreDetailList.length == 0) {
                $("#info").css("border", "none").html('<p class="noResult" style="margin-top:0px;padding-top:20px;"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">未查询到任何数据</p>');
            } else {
                $("#info").append(
                    "<tr>" +
                    "<th>序号</th>" +
                    "<th>流水号</th>" +
                    "<th>交易日期</th>" +
                    "<th>交易类型</th>" +
                    "<th>支出类型</th>" +
                    "<th>交易方名称</th>" +
                    "<th>收支明细</th>" +
                    "</tr>");
            }
            $.each(data.scoreDetailList, function (i, e) {
                $("#info").append(
                    "<tr>" +
                    "<td>" + (i + 1) + "</td>" +
                    "<td>" + getOrderId(e.id, e.orderId) + "</td>" +
                    "<td>" + e.createTime + "</td>" +
                    "<td>" + getStringutil(e.transferType, 'type') + "</td>" +
                    "<td>" + e.paymentType + "</td>" +
                    "<td>" + getStringutil(e.targetName) + "</td>" +
                    "<td>" + getBalanceDetail(e.paymentType, e.income, e.output) + "</td>" +
                    "</tr>"
                );
            });
            //分页
            if ($(".tcdPageCode").html() == "") {
                createPage(data.sumpage, data.nowpage, paymentType, transferType, startTime, endTime);
            }
            $('.loadingBox').hide();
        },
        "json"
    );
}

function getOrderId(id, oid) {
    if (oid == null || oid == 'null' || oid == '') {
        return id;
    }
    return oid;
}

function getStringutil(s, type) {
    if (s == null || s == 'null') {
        if (type) {
            return '未知类型';
        }
        return '未知';
    }
    return s;
}

function getBalanceDetail(type, income, output) {
    if (!isNotEmpty(output) && isNotEmpty(income)) { //output为空，income不为空
        if (type == '支出' || type == -1)
            return "<span style='color: red'>" + "-" + (income - 0).toFixed(2) + "</span>";
        if (type == '收入' || type == 1)
            return "<span style='color: green'>" + "+" + (income - 0).toFixed(2) + "</span>";
    } else if (isNotEmpty(output) && !isNotEmpty(income))
        return "<span style='color: red'>" + "-" + (output - 0).toFixed(2) + "</span>";
}

/**
 * 按条件查询
 * 按 页码 跳转
 * @param page
 */
function getInfo(page) {
    $('.loadingBox').show();
    if (page) {
        page = 1;
    }
    var paymentType = $("[name='paymentType']").val();
    var transferType = $("[name='transferType']").val();
    var startTime = $("#datepicker").val();
    var endTime = $("#datepicker2").val();
    if (startTime > endTime) {
        alertBox("开始时间不能大于结束时间", "error");
        $('.loadingBox').hide();
    } else {
        $("#info").empty();
        ajaxscoreDetail(page, paymentType, transferType, startTime, endTime);
    }
}

function createPage(pages, pageNum, paymentType, transferType, startTime, endTime) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                ajaxscoreDetail(p, paymentType, transferType, startTime, endTime);
            }
        });
    }
}
